package com.zhang.controller;


import com.github.pagehelper.PageInfo;
import com.zhang.domain.*;
import com.zhang.query.PetQuery;
import com.zhang.service.IPetService;
import com.zhang.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author zh
 * @create 2022-02-14 9:06
 */

@RestController
@RequestMapping("pet")
public class PetController {
    @Autowired
    private IPetService service;
    @PutMapping
    private AjaxResult addOrUpdate(@RequestBody Pet m){
        if(m.getSearchMsg().getId()!=null){
            try {
                service.update(m);
                return AjaxResult.me();
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.me(false,"修改失败");
            }
        }else {
            try {
                service.save(m);
                return AjaxResult.me();
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.me(false,"添加失败");
            }
        }
    }
    @DeleteMapping("/{id}")
    private AjaxResult delete(@PathVariable("id") Long id){
        try {
            service.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me(false,"删除失败");
        }
    }
    @PostMapping
    private AjaxResult getAllData(@RequestBody PetQuery query){
        try {
            PageInfo<Pet> pageInfo = service.queryData(query);
            return AjaxResult.me(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me(false,"查询失败");
        }
    }
    @GetMapping("/selAllPetType")
    private AjaxResult selAllEmp(){
        try {
            List<PetType> petTypes = service.selAllPetType();
            return AjaxResult.me(petTypes);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me(false,"查询失败");
        }
    }
    @PostMapping("/petDetail")
    private AjaxResult petDetail(@RequestBody Map<String,Integer> map) {
        try {
            PetDetail petDetail = service.getPetDetail(map.get("id").longValue());
            return AjaxResult.me(petDetail);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me(false,"查询失败");
        }
    }
    @GetMapping("/loadById/{id}")
    private AjaxResult loadById(@PathVariable("id") Long id) {
        try {
            Pet pet = service.loadById(id);
            return AjaxResult.me(pet);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me(false,"查询失败");
        }
    }
    @PostMapping("/updateState")
    private AjaxResult updateState(@RequestBody Map<String,Integer> map) {
        try {
            service.updateState(map.get("id").longValue());
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me(false,"查询失败");
        }
    }
    @PostMapping("/addOrder")
    private AjaxResult addOrder(@RequestBody PetAcq petAcq) {
        try {
            service.addOrder(petAcq);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me(false,"查询失败");
        }
    }

}
